package com.file.controller;

import com.file.entity.PaymentResult;
import com.file.service.PaymentService;
import com.file.utiltools.basic.ValueUtil;
import com.file.utiltools.error.ToolsException;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/pay")
public class PayController {


//            (required = false)//等他用的时候自动注入
    @Autowired
    private PaymentService paymentService;

    @RequestMapping(method = RequestMethod.POST)
    public String pay(String orderNumber, Double amount,Integer userId, String title, String description) {
        //查询本地交易记录
        if(ValueUtil.isEmpity(orderNumber)){
            return ValueUtil.toJson(HttpStatus.SC_INTERNAL_SERVER_ERROR,"订单号不能为空");
        }
        PaymentResult paymentResult = paymentService.pay(orderNumber, amount, title, description, userId);
        return ValueUtil.toJson(HttpStatus.SC_CREATED,paymentResult);

    }

    @RequestMapping(value = "/app", method = RequestMethod.POST)
    public String appPay(String orderNumber, Double amount,Integer userId, String title, String description, HttpServletRequest request, HttpServletResponse response) {
//
//        （1）用户在客户端中点击购买商品，客户端发起订单生成请求到商户后台；
//（2）商户后台收到订单生成请求后，按照《手机控件支付产品接口规范》组织并推送订单信息至银联后台；
//（3）银联后台接收订单信息并检查通过后，生成对应交易流水号（即TN），并回复交易流水号至商户后台（应答要素：交易流水号等）；
//（4）商户后台接收到交易流水号，将交易流水号返回给客户端；
//（5）客户端通过交易流水号（TN）调用支付控件；
//（6）用户在支付控件中输入相关支付信息后，由支付控件向银联后台发起支付请求；
//（7）支付成功后，银联后台将支付结果通知给商户后台；
//（8）银联将支付结果通知支付控件；
//（9）支付控件显示支付结果并将支付结果返回给客户端；
          /*  TransactionHistory transactionHistory = transactionService.findByOrderId(orderNumber);
            ValueUtil.verifyExist(transactionHistory, "该交易已完成");
            Payment pay = Payment.getPayment(payment);
            PaymentService paymentService = payFactory.getInstance(pay);*/
            PaymentResult paymentResult = null;
            try {
                paymentResult = paymentService.appPay(orderNumber, amount, title, description, userId, request);
                return ValueUtil.toJson(paymentResult.getData());
            } catch (ToolsException e) {
                return ValueUtil.toError(e.getCode(), e.getMessage());
            }
    }


    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public String refund(String orderNumber, Double payAmount, Double refundAmount, String refundNumber, String title, String description,String queryId ) {

        try {
            return ValueUtil.toJson(paymentService.refund(orderNumber,refundNumber, payAmount, refundAmount, title, description,queryId));
        } catch (Exception e) {
            e.printStackTrace();
            return ValueUtil.toError(HttpStatus.SC_INTERNAL_SERVER_ERROR, "退款失败");
        }

    }

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public String queryDetail(String orderNumber) {
        try {
            return ValueUtil.toJson(HttpStatus.SC_CREATED,paymentService.queryDetail(orderNumber));
        } catch (Exception e) {
            return ValueUtil.toError(HttpStatus.SC_INTERNAL_SERVER_ERROR, "查询失败");
        }
    }

}
